package com.gu.service.impl;

import Utils.JwtUtils;
import Utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.exception.DiyException;
import com.gu.entity.UcenterMember;
import com.gu.entity.vo.LoginVo;
import com.gu.entity.vo.RegisterVo;
import com.gu.service.UcenterMemberService;
import com.gu.mapper.UcenterMemberMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 *
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember>
        implements UcenterMemberService {

    @Resource
    private RedisTemplate<String,Object> redisTemplate ;

    @Resource
    private UcenterMemberMapper mapper ;

    @Override
    public String login(LoginVo loginVo) {
        String phoneNum = loginVo.getMobile();
        String password = loginVo.getPassword();

        if (StringUtils.isEmpty(phoneNum) || StringUtils.isEmpty(password)) {
            throw new DiyException("500", "账号或密码不能为空");
        }
        QueryWrapper<UcenterMember> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile", phoneNum);
        memberQueryWrapper.eq("password", MD5.encrypt(password));
        int count = this.count(memberQueryWrapper);

        if (count > 0) {
            //登陆成功，需要生成token
            UcenterMember ucenterMember = this.getOne(memberQueryWrapper);

            //此处验证该用户账号是否被封禁；
            Boolean isDisabled = ucenterMember.getIsDisabled();
            if (isDisabled){
                throw new DiyException("600", "该账号已被封禁！");
            }

            String id = ucenterMember.getId();
            String nickname = ucenterMember.getNickname();
            String token = JwtUtils.getJwtToken(id, nickname);
            return token;
        } else {
            throw new DiyException("500", "非法登陆，请先注册");
        }
    }

    @Override
    public boolean register(RegisterVo registerVo) {
        String phoneNum = registerVo.getMobile();
        String password = registerVo.getPassword();
        String messageCode = registerVo.getMessageCode();
        String nickname = registerVo.getNickname();

        //多重验证功能:
        if (StringUtils.isEmpty(phoneNum) || StringUtils.isEmpty(password)) {
            throw new DiyException("500", "账号或密码错误");
        }

        if (!redisTemplate.opsForValue().get(phoneNum).equals(messageCode)){
            throw new DiyException("500", "验证码错误");
        }

        QueryWrapper<UcenterMember> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile", phoneNum);
        int count = this.count(memberQueryWrapper);
        if (count>0) {
            throw new DiyException("500", "账号已经存在！");
        }

        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(phoneNum);
        ucenterMember.setPassword(MD5.encrypt(password));
        //设置一个默认头像：
        ucenterMember.setAvatar("https://edu-yuyikao8002.oss-cn-hangzhou.aliyuncs.com/2022/05/19/eca3ed9b922a4bfc8971eb893914c7c2file.png");
        ucenterMember.setNickname(nickname);
        boolean result = this.save(ucenterMember);

        return result;
    }

    //根据token来获取用户的登陆信息：
    @Override
    public  UcenterMember getLoginInfoByToken(HttpServletRequest request) {
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember ucenterMember = this.getById(userId);
        return ucenterMember;
    }

    @Override
    public Integer getReNum(String dateTime) {
        Integer nums = mapper.getRegisterNums(dateTime);
        return nums;
    }
}




